package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.PurchaseDetailsMapper;
import com.spark.incoicings.dao.ReceiptDetailMapper;
import com.spark.incoicings.dao.ReturnDetailMapper;
import com.spark.incoicings.service.ReceiptDetailService;
import com.spark.invoicings.entity.PurchaseDetail;
import com.spark.invoicings.entity.ReceiptDetail;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ReceiptDetailServiceImpl
        extends ServiceImpl<ReceiptDetailMapper, ReceiptDetail>
        implements ReceiptDetailService {

    @Resource
    private ReceiptDetailMapper receiptDetailMapper;

    @Resource
    private PurchaseDetailsMapper purchaseDetailMapper;

    @Autowired
    private ReturnDetailMapper returnDetailMapper;

    /**
     * 根据id查询到货单明细
     * @param id
     * @return
     */
    @Override
    public ReceiptDetail selectReceiptDetailByIds(Integer id) {
        ReceiptDetail receiptDetail = receiptDetailMapper.selectReceiptDetailByIds(id);
        PurchaseDetail purchaseDetail = purchaseDetailMapper.selectPurchaseDetailsById(receiptDetail.getPurchaseDetailId());
        receiptDetail.setPurchaseDetail(purchaseDetail);
        return receiptDetail;
    }

    /**
     * 更新到货单明细
     * @param receiptDetail
     * @return
     */
    @Override
    public boolean updateReceiptDetail(ReceiptDetail receiptDetail) {
        // 根据采购明细id获取待到货数量
        PurchaseDetail purchaseDetail = purchaseDetailMapper.selectById(receiptDetail.getPurchaseDetailId());
        // 判断待到货单的待到货数量 是否超出采购订单明细的待到货数量
        if (receiptDetail.getReceivedQuantity() > purchaseDetail.getPendingArrivalQuantity()) {
            return false;
        }
        return receiptDetailMapper.updateById(receiptDetail) > 0;
    }

    /**
     * 添加到货单明细
     * @param receiptDetail
     * @return
     */
    @Override
    public Result addReceiptDetail(ReceiptDetail receiptDetail) {
        // 根据采购明细id获取待到货数量
        PurchaseDetail purchaseDetail = purchaseDetailMapper.selectById(receiptDetail.getPurchaseDetailId());
        // 判断待到货单的待到货数量 是否超出采购订单明细的待到货数量
        if (receiptDetail.getReceivedQuantity() > purchaseDetail.getPendingArrivalQuantity()) {
            return Result.error(500,"超出采购订单明细的待到货数量");
        }
        receiptDetailMapper.insert(receiptDetail);
        return Result.success(200, "success", receiptDetail.getReceiptDetailId());
    }

    /**
     * 查询所有采购到货明细
     * @param map
     * @return
     */
    @Override
    public List<ReceiptDetail> selectReceiptDetailAll(Map<String, Object> map) {
        List<ReceiptDetail> receiptDetails = receiptDetailMapper.selectReceiptDetailAll(map);
        for (ReceiptDetail receiptDetail : receiptDetails) {
            PurchaseDetail purchaseDetail = purchaseDetailMapper.selectPurchaseDetailsById(receiptDetail.getPurchaseDetailId());
            receiptDetail.setPurchaseDetail(purchaseDetail);
        }
        return receiptDetails;
    }
}
